package com.example.questionnaire.controller;


import com.example.questionnaire.common.ServerResponse;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.service.AppidAndSecretService;
import com.example.questionnaire.service.QuestionnaireAgreementService;
import com.example.questionnaire.service.QuestionnaireSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;



@RestController
@RequestMapping("/QuestionnaireAgreementController")
@Api(tags = "问卷知情同意书")
public class QuestionnaireAgreementController {

    @Autowired
    private QuestionnaireAgreementService questionnaireAgreementService;
    @Autowired
    AppidAndSecretService appidAndSecretService;

    /**
     * 知情同意书设置
     *
     * @param formid（问卷id）
     * @param title（标题）
     * @param content（正文）
     * @param showNum（显示频次）
     * @param agree（设置同意按钮文字）
     * @param disagree（设置不同意按钮文字）
     * @param shortestReadingTime（是否开启阅读时间）
     * @param seconds（阅读的秒数）
     */
    @PostMapping("/agreement")
    @ApiOperation(value = "知情同意书设置", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse agreement(@RequestParam String formid,
                                    @RequestParam String title,
                                    @RequestParam String content,
                                    @RequestParam int showNum,
                                    @RequestParam String agree,
                                    @RequestParam String disagree,
                                    @RequestParam boolean shortestReadingTime,
                                    @RequestParam boolean isDisagree,
                                    @RequestParam int seconds) throws ParseException {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }

        boolean b = questionnaireAgreementService.agreement(formid, title, content, showNum, agree, disagree, shortestReadingTime, isDisagree,seconds);
        if (b) {
            return ServerResponse.createBySuccess("设置成功");
        }
        return ServerResponse.createByError("设置失败");


    }

    /**
     * @param formid（问卷id）

     */
    @GetMapping("/getAgreement")
    @ApiOperation(value = "获取知情同意书", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse getAgreement(@RequestParam String formid){


        return ServerResponse.createBySuccess(questionnaireAgreementService.getAgreement(formid));

    }

}
